#include "GS_PlayMonopoly.h"
#include "GameCore\Device_Base.h"
#include "GameCore\Screen.h"
#include "GameCore\PhysicsManager.h"
#include "GameCore\FontManager.h"
#include "GameCore\StringManager.h"

void GS_PlayMonopoly::Init()
{
	camera = new Camera();
	camera->SetCameraLookAt(500.0f, 1700.0f, 1700.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
	projection3D.SetPerspectiveFovy(0.4f, Screen::GetInstance()->GetRatio(), 0.1f, 3000.0f);
	Device_Base::GetInstance()->SetProjectionMatrix(projection3D);
	GenerateBoard();
}
void GS_PlayMonopoly::Release()
{
	SafeDelete(vboBoard);
	SafeDelete(camera);
}
void GS_PlayMonopoly::Update(float timeTotal, float timeDelta)
{
	
}
void GS_PlayMonopoly::Render()
{
	Device_Base::GetInstance()->SetClearColorBuffer(0.0f, 0.0f, 0.0f, 1.0f);
	Device_Base::GetInstance()->ClearColorDepthStencilBuffer(true, true, false);
	//DrawBoard();
	Program* textPrg = ShaderManager::GetInstance()->GetProgramFromFile("Monopoly_board_vs.txt", "Monopoly_board_fs.txt");
	Device_Base::GetInstance()->UseProgram(textPrg);
	Device_Base::GetInstance()->PushMatrix();
	Device_Base::GetInstance()->SetCameraActivated(camera);
	
	static float angle = 0.0f;
	angle += 0.02f;
	Device_Base::GetInstance()->RotateOy(angle);
	Device_Base::GetInstance()->Scale(4.0, 4.0, 4.0);
	Device_Base::GetInstance()->EnableDepthTest();
	Device_Base::GetInstance()->DisableFaceCulling();
	character->Render();
	Device_Base::GetInstance()->PopMatrix();
}

void GS_PlayMonopoly::DrawBoard()
{
	Program* textPrg = ShaderManager::GetInstance()->GetProgramFromFile("Monopoly_board_vs.txt", "Monopoly_board_fs.txt");
	Device_Base::GetInstance()->UseProgram(textPrg);
	//Device_Base::GetInstance()->SetProjectionOrthoCenterMatrix(Device_Base::GetInstance()->GetWidth()
	//	, Device_Base::GetInstance()->GetHeight(), 0.01f, 100.0f);
	Device_Base::GetInstance()->PushMatrix();
	Device_Base::GetInstance()->SetCameraActivated(camera);
	Device_Base::GetInstance()->RotateOx(-1.57f);
	spriteBoard->DrawFrame(0, 0, 0);
	Device_Base::GetInstance()->PopMatrix();
}

void GS_PlayMonopoly::GenerateBoard()
{
	vboBoard = new VBO(VBO::VBO_TARGET_ARRAY_BUFFER);
	float x = 20.0f;
	float y = 0.0f;
	float z = 20.0f;
	float vertices[30] = { -x, y, -z, 0.0f, 1.0f,
		-x, y, z, 0.0f, 0.0f,
		x, y, z, 1.0f, 0.0f,
		-x, y, -z, 0.0f, 1.0f,
		x, y, z, 1.0f, 0.0f,
		x, y, -z, 1.0f, 1.0f
	};
	vboBoard->usage = VBO::VBO_USAGE_STATIC_DRAW;
	vboBoard->SetAttributeOffset(ATTRIBUTE_FLOAT3_POSITION, 0);
	vboBoard->SetAttributeOffset(ATTRIBUTE_FLOAT2_TEXCOORD_DIFFUSE, 3 * sizeof(float));
	Device_Base::GetInstance()->CreateVBO(vboBoard, (const char*)vertices, sizeof(vertices));
	spriteBoard = SpriteManager::GetInstance()->LoadSpriteFromXMLFile("Board2.sprite");
	character = new GameObject3D();
	character->LoadFromFile("D:\\SVN_WC\\CAC\\trunk\\prj\\Debug\\sdcard\\3dsmax\\box.fbx");
}

void GS_PlayMonopoly::OnTouchDown(unsigned int id, float x, float y)
{

}
void GS_PlayMonopoly::OnTouchUp(unsigned int id, float x, float y)
{

}
void GS_PlayMonopoly::OnTouchMove(unsigned int id, float x, float y)
{

}
void GS_PlayMonopoly::CameraFollowCharacter(GameObject3D* character)
{
	Vector3f dest = character->GetPosition();
	Vector3f normalized = Normalize(dest) * 500.0f;
	Vector3f eye = dest + normalized + Vector3f(0.0f, 200.0f, 0.0f);
	camera->SetCameraLookAt(eye.x, eye.y, eye.z, dest.x, dest.y, dest.z, 0.0f, 1.0f, 0.0f);
}